package com.elive.controller.customer;

import com.elive.common.Const;
import com.elive.common.ServerResponse;
import com.elive.pojo.User;
import com.elive.service.IUserService;
import com.elive.util.SessionUtil;
import com.elive.vo.UserSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wuyin on 2018/4/7.
 */
@Controller
@RequestMapping("/customer/user")
public class UserController {

    @Autowired
    private IUserService iUserService;

    private static Map sessionMap = new HashMap();

    // 注册用户（微信小程序端）
    @RequestMapping(value = "register.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> register(User user) {
        if(user.getUsername() == null){
            return ServerResponse.createByErrorMessage("用户名不能为空");
        }
        return iUserService.registerCustomer(user);

    }

    // 登录 (微信小程序端)
    @RequestMapping(value = "login.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<UserSession> login(User user, HttpSession session){
        if(user.getId() == null){
            return ServerResponse.createByErrorMessage("登录失败");
        }
        ServerResponse<User> response = iUserService.customerLogin(user.getId());
        if(response.isSuccess()){

            User responseUser = response.getData();
            session.setAttribute(Const.CURRENT_USER,responseUser);
            UserSession userSession = new UserSession();
            userSession.setUser(responseUser);
            userSession.setSessionId(session.getId());
            SessionUtil.sessionMap.put(session.getId(),session);
            return ServerResponse.createBySuccess(userSession);
        }else {
            return ServerResponse.createByErrorMessage("登录失败");
        }
    }




    // 获取用户信息(微信小程序端)
    @RequestMapping(value = "getUserInfo.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getUserInfo(String sessionId,HttpSession httpSession) {
        if(SessionUtil.sessionMap.containsKey(sessionId)){
            httpSession = SessionUtil.sessionMap.get(sessionId);
        }
        User currentUser = (User) httpSession.getAttribute(Const.CURRENT_USER);
        if(currentUser == null) {
            return ServerResponse.createByErrorMessage("用户未登录");
        }else {
            return iUserService.getUserInfo(currentUser.getId());
        }
    }

    // 更新用户的信息（微信客户端）
    @RequestMapping(value = "updateUser.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> updateUser(String sessionId, User user,HttpSession httpSession){
        if(SessionUtil.sessionMap.containsKey(sessionId)){
            httpSession = SessionUtil.sessionMap.get(sessionId);
        }

        User currentUser = (User)httpSession.getAttribute(Const.CURRENT_USER);
        // 如果未登录
        if(currentUser == null) {
            return ServerResponse.createByErrorMessage("用户未登录");
        } else {
            user.setId(currentUser.getId());
            return iUserService.updateCustomerUser(user);
        }
    }

    @RequestMapping(value = "isRegister.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> isRegister(String username){
        return iUserService.isRegister(username);
    }

}
